#pragma once
#include <osg/Camera>
#include "OImage.h"

class ODrawCallback : public osg::Camera::DrawCallback {
public:
	ODrawCallback(OImage* fboImage) : m_fboImage(fboImage) {}

	~ODrawCallback() { std::cout << ">>> ~ODrawCallback" << std::endl; }

	virtual void operator()(osg::RenderInfo& renderInfo) const override {
		static int i = 0;
		if (++i < 10) return;
		if (m_isSaved)
			return;
		if (!m_fboImage || !m_fboImage->valid()) {
			std::cerr << "m_fboImage is invalid!" << std::endl;
			return;
		}
		bool success = m_fboImage->saveToLocal("rtt_output.png");
		if (!success) {
			std::cerr << "Texture saved fail!" << std::endl;
			return;
		}
		m_isSaved = true;
		std::cout << "Texture saved to rtt_output.png" << std::endl;
	}

private:
	OImage* m_fboImage;
	mutable bool m_isSaved = false;
};